#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Author: FQ
Module Description: rank_award_utils ... 
File: rank_award_utils.py
Date: 2019/2/28
"""
from pyshare.const.enums.activity import ActivityRecordStatus
from pyshare.utils.time_utils import getCT
from pyshare.framework.error_handle import CrossGameException
from pyshare.bll.utils.cross.activity_utils import get_award_index
from pyshare.const.enums.cross_chess import ChessReturnStat


class ActivityRankAward(object):
    """
    跨服活动榜单奖励方法
    """
    @staticmethod
    def get_award_attr():
        raise Exception('Not ReWrite ActivityRankAward.get_award_attr')

    @staticmethod
    def raise_award_already():
        raise Exception('Not ReWrite ActivityRankAward.raise_award_already')

    @staticmethod
    def raise_not_achieve():
        raise Exception('Not ReWrite ActivityRankAward.raise_not_achieve')

    @classmethod
    def is_award(cls, record):
        """
        是否已领奖
        :param record:
        :return:
        """
        if getattr(record, cls.get_award_attr()) == ActivityRecordStatus.Award:
            cls.raise_award_already()

    @classmethod
    def is_achieve(cls, rank, range_list, award_list):
        """是否达成领奖条件"""
        award_index = get_award_index(rank, range_list)
        if award_index > -1:
            # 可领取
            return award_list[award_index]
        cls.raise_not_achieve()

    @classmethod
    def is_achieve_with_index(cls, rank, range_list, award_list):
        """是否达成领奖条件"""
        award_index = get_award_index(rank, range_list)
        if award_index > -1:
            # 可领取
            return award_list[award_index], award_index
        cls.raise_not_achieve()

    @classmethod
    def set_award(cls, record):
        setattr(record, cls.get_award_attr(), ActivityRecordStatus.Award)

    @classmethod
    def get_achieve_status(cls, rank, range_list, has_award, end_time):
        """
        获取领奖状态
        :param rank:
        :param range_list:
        :param has_award:
        :param end_time: 活动结束时间
        :return:
        """
        if end_time > getCT():
            return ActivityRecordStatus.OnGoing
        if has_award == ActivityRecordStatus.Award:
            return ActivityRecordStatus.Award
        award_index = get_award_index(rank, range_list)
        if award_index > -1:
            return ActivityRecordStatus.Done
        return ActivityRecordStatus.OnGoing

    @classmethod
    def is_show_red(cls, rank, range_list, has_award, end_time):
        if cls.get_achieve_status(rank, range_list, has_award, end_time) == ActivityRecordStatus.Done:
            return 1
        return 0


class CrossChessPersonalRankAward(ActivityRankAward):
    """
    跨服棋局个人榜单奖励方法
    """

    @staticmethod
    def get_award_attr():
        return 'is_get_personal_award'

    @staticmethod
    def raise_award_already():
        raise CrossGameException('', stat=ChessReturnStat.AwardAlready)

    @staticmethod
    def raise_not_achieve():
        raise CrossGameException('', stat=ChessReturnStat.NotAchieveAward)


class CrossChessTeamRankAward(CrossChessPersonalRankAward):
    """
    跨服棋局队伍榜单奖励方法
    """

    @staticmethod
    def get_award_attr():
        return 'is_get_team_award'